import { wisdomLogin, userInfo, changeLoginPassword } from '@/api/user'
import router from '@/router'
import { setToken, getToken, removeToken } from '@/utils/auth'
import { Message } from 'element-ui'
const state = {
  token: getToken() || '', // 获取token
  userInfo: {} // 存储用户名 和 id
}
const getters = {

}
const mutations = {
  setToken(state, token) {
    state.token = token // 存储token到vuex
    setToken(token) // 存储token到本地
  },
  userInfo(state, name) { // 存储用户名和 id
    state.userInfo = name
  },
  loginOut(state) { // 退出登录
    state.token = ''
    state.userInfo = {}
    removeToken()
  }
}
const actions = {
  async getWisdomLogin(context, data) { // 登录请求
    const res = await wisdomLogin(data)
    Message({ type: 'success', message: '登录成功' })
    context.commit('setToken', res.data.token)
    setTimeout(() => {
      router.push('/')
    }, 1000)
  },
  async getUserInfo(context) { // 调用获取用户名和 ID 的接口
    const res = await userInfo()
    context.commit('userInfo', res.data[0])
  },
  async confirmAlter(context, data) { // 修改密码请求
    await changeLoginPassword(data)
  }
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
